## *************************************
##         B8 Party Secretary       
##        Hao Zhang & Ye Zhang
##             2025/7/24
## *************************************

# load packages
library(lfe)
library(stargazer)
library(tidyverse)
library(readstata13)

# load data
rm(list = ls())
load("../analysis data/firm analysis.RData")

# load party secretary data
setwd("../raw data")
cs <- read.dta13("city secretary 1990-2015.dta") %>% 
  select(citycode, year, code_sc, begin_sc, end_sc, educ_sc, ethnicity_sc, gender_sc)
firm <- left_join(firm, cs, by = c("citycode", "year"))

# run inverse hyperbolic sine
ihs <- function(x) {
  y <- log(x + sqrt(x ^ 2 + 1))
  return(y)
}

# unemployment insurance # warnings due to limited observations for certain categories
m1 <- felm(insurance_dummy ~ gdp_pressure + ihs(employment) + 
             ihs(wage) + ihs(output1) + ihs(profit) +  
             log(FDI + 1) + gdpgrowth + log(gdp/population) +
             ihs(deficit/gdp) +  ihs(capacity) + tax + educ + 
             ethnicity  + gender + begin + end + connection +
             educ_sc + ethnicity_sc  + gender_sc + 
             begin_sc + end_sc | frdm + year  | 0 | citycode, data = firm)

firm1 <- firm %>% filter(rate1 > 0)
m2 <- felm(log(rate1) ~ gdp_pressure + ihs(employment) + 
             ihs(wage) + ihs(output1) + ihs(profit) +  
             log(FDI + 1) + gdpgrowth + log(gdp/population) +
             ihs(deficit/gdp) +  ihs(capacity) + tax + educ + 
             ethnicity  + gender + begin + end + connection +
             educ_sc + ethnicity_sc  + gender_sc + 
             begin_sc + end_sc | frdm + year | 0 | citycode, data = firm1)

# medical insurance
m3 <- felm(medical_dummy ~ gdp_pressure + ihs(employment) + 
             ihs(wage) + ihs(output1) + ihs(profit) +  
             log(FDI + 1) + gdpgrowth + log(gdp/population) +
             ihs(deficit/gdp) +  ihs(capacity) + tax + educ + 
             ethnicity  + gender + begin + end + connection +
             educ_sc + ethnicity_sc  + gender_sc + 
             begin_sc + end_sc | frdm + year  | 0 | citycode, data = firm)

firm2 <- firm %>% filter(rate2 > 0)
m4 <- felm(log(rate2) ~ gdp_pressure + ihs(employment) + 
             ihs(wage) + ihs(output1) + ihs(profit) +  
             log(FDI + 1) + gdpgrowth + log(gdp/population) +
             ihs(deficit/gdp) +  ihs(capacity) + tax + educ + 
             ethnicity  + gender + begin + end + connection +
             educ_sc + ethnicity_sc  + gender_sc + 
             begin_sc + end_sc | frdm + year  | 0 | citycode, data = firm2)

# output regression table
stargazer(m1, m2, m3, m4, title = "Interjurisdictional Competition and Social Insurances (Party Secretary)", 
          dep.var.labels = c("Unemployment", "Unemployment", "Health and Pension", "Health and Pension"),
          omit.stat = c("rsq", "adj.rsq", "ser"), 
          font.size = "small", 
          add.lines = list(c("Mayor Characteristics", "Y", "Y", "Y", "Y"),
                           c("Firm Characteristics", "Y", "Y", "Y", "Y"), 
                           c("Firm Fixed Effects", "Y", "Y", "Y", "Y"), 
                           c("Year Fixed Effects", "Y", "Y", "Y", "Y")))
